//
//  TVCellOfDoTest.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/2.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "TVCellOfDoTest.h"
#import "BaseCollectionView.h"
#import "CVCellOfDoTest.h"
#import "MJmacro.h"
#import "DataOfDoTestChoiceButton.h"
#import "RootClassOfDoTextChoiceButton.h"
#import "ZWYNetTool.h"
#import "UIImageView+WebCache.h"
#import "VCOfClassifyTest.h"
#import "VCOfDoTest.h"
@interface TVCellOfDoTest()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)BaseCollectionView *doTestButtonCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) RootClassOfDoTextChoiceButton *rootClassOfDoTextChoiceButton;
@end
@implementation TVCellOfDoTest
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self handleData];
        [self createCollectionButton];
    }
    return self;
}
#pragma mark - 数据处理
- (void)handleData{
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/yi/test-category-list.json" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _rootClassOfDoTextChoiceButton = [RootClassOfDoTextChoiceButton modelDic:result];
        [self.doTestButtonCollectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}
#pragma mark - 创建 CollectionView 的 button
- (void)createCollectionButton{
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _doTestButtonCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    [self.contentView addSubview:_doTestButtonCollectionView];
    _doTestButtonCollectionView.delegate = self;
    _doTestButtonCollectionView.dataSource = self;
    [_doTestButtonCollectionView registerNib:[UINib nibWithNibName:@"CVCellOfDoTest" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CVCellOfDoTest"];
}
#pragma mark - 布局方法
- (void)layoutSubviews{
    [super layoutSubviews];
    _doTestButtonCollectionView.frame = self.contentView.bounds;
    _doTestButtonCollectionView.backgroundColor = [UIColor whiteColor];
    _layout.itemSize = CGSizeMake(self.contentView.bounds.size.width / 5, self.contentView.bounds.size.width / 5);
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 0;
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
#pragma mark - 代理方法 item 的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _rootClassOfDoTextChoiceButton.data.count;
}
#pragma mark - 代理方法 cell 的赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CVCellOfDoTest *cellOfDoTest = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfDoTest" forIndexPath:indexPath];
    [cellOfDoTest.myImageVIew sd_setImageWithURL:[NSURL URLWithString:_rootClassOfDoTextChoiceButton.data[indexPath.item].cover] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
    cellOfDoTest.myLabel.text = _rootClassOfDoTextChoiceButton.data[indexPath.item].name;
    return cellOfDoTest;
}
#pragma mark - collectionView 点击实现方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    VCOfClassifyTest *vcOfClassifyTest = [[VCOfClassifyTest alloc] init];
    vcOfClassifyTest.category_id = _rootClassOfDoTextChoiceButton.data[indexPath.item].ID;
    VCOfDoTest *vcOfDoTest = (VCOfDoTest *)self.nextResponder.nextResponder.nextResponder.nextResponder;
    [vcOfDoTest.navigationController pushViewController:vcOfClassifyTest animated:YES];

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
